package tongcheng.com.tongcheng.network;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;

import tongcheng.com.tongcheng.utils.ShowToast;


public class CallServer {

    private static CallServer instance;

    /**
     * 请求队列。
     */
    private RequestQueue requestQueue;

    private CallServer() {
        requestQueue = NoHttp.newRequestQueue(3);
    }

    /**
     * 请求队列。
     */
    public synchronized static CallServer getInstance() {
        if (instance == null)
            synchronized (CallServer.class) {
                if (instance == null)
                    instance = new CallServer();
            }
        return instance;
    }

    /**
     * 添加一个请求到请求队列。
     *
     * @param what      用来标志请求, 当多个请求使用同一个Listener时, 在回调方法中会返回这个what。
     * @param    。
     * @param   。
     */
    public  void addPost(Context context,MyRequest request, int what, final OnCallBack callBack) {
       // Request<String> stringRequest = NoHttp.createStringRequest(Url, RequestMethod.POST);
      /*  request.requestMethod = RequestMethod.POST;*/

        final ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("请稍后,正在加载中");
        progressDialog.show();
        requestQueue.add(what, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response response) {
                if (response.get() != null) {
                    Log.e("login", (String) response.get());
                    try {
                        JSONObject jsonObject = new JSONObject((String) response.get());
                       String msg= jsonObject.getString("msg");
                        if (msg != null) {
                            ShowToast.showToast(msg);
                        }
                    } catch (Exception e) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }

                    if (progressDialog != null) {
                        progressDialog.dismiss();
                    }
                    callBack.OnSuccess(what,(String) response.get(),response.get());
                }

            }

            @Override
            public void onFailed(int what, Response response) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }

                ShowToast.showToast("网络访问出错");
                if (response.getException() != null) {
                    if (response.getException().getMessage() != null) {
                        Log.e("httpex", response.getException().getMessage().toString());
                    }

                }
            }

            @Override
            public void onFinish(int what) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }

            }
        });
       // requestQueue.add(what, request, listener);
    }


    /**
     * 添加一个请求到请求队列。
     *
     * @param what      用来标志请求, 当多个请求使用同一个Listener时, 在回调方法中会返回这个what。
     * @param    。
     * @param   。
     */
    public  void addGet(int what, final OnCallBack callBack,MyRequest request) {
       // Request<String> stringRequest = NoHttp.createStringRequest(Url, RequestMethod.GET);
        //request.setDefineRequestBody(fileStream, Headers.HEAD_VALUE_CONTENT_TYPE_OCTET_STREAM);


        requestQueue.add(what, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response response) {
                if (response.get() != null) {
                    callBack.OnSuccess(what,(String) response.get(),response.get());
                }

            }

            @Override
            public void onFailed(int what, Response response) {
                if (response.getException() != null) {
                    Log.e("httpex", response.getException().getMessage().toString());
                }
            }

            @Override
            public void onFinish(int what) {

            }
        });
        // requestQueue.add(what, request, listener);
    }

    /**
     * 取消这个sign标记的所有请求。
     * @param sign 请求的取消标志。
     */
    public void cancelBySign(Object sign) {
        requestQueue.cancelBySign(sign);
    }

    /**
     * 取消队列中所有请求。
     */
    public void cancelAll() {
        requestQueue.cancelAll();
    }


}